﻿using System;

namespace BiggerElementThanNeighbours
{
    class BiggerElementThanNeighbours
    {
        static bool IsValidIndex(int[] array, int index)
        {
            return index < array.Length && 0 <= index;
        }

        static bool IsBiggerThanNeighbors(int[] array, int index)
        {
            if (!IsValidIndex(array, index))
            {
                throw new FormatException(string.Format("{0} is not a valid index.", index));
            }

            bool leftNeightbour = IsValidIndex(array, index - 1) ? array[index] > array[index - 1] : true;
            bool rightNeightbour = IsValidIndex(array, index + 1) ? array[index] > array[index + 1] : true;

            return leftNeightbour && rightNeightbour;
        }

        static int FirstIndexNumberBiggerThanNeighbors(int[] array)
        {
            for (int index = 0, length = array.Length; index < length; index++)
            {
                if (IsBiggerThanNeighbors(array, index))
                {
                    return index;
                }
            }
            return -1;
        }

        static void Main(string[] args)
        {
            int[] array = { 1, 3, 1 };
            Console.WriteLine(FirstIndexNumberBiggerThanNeighbors(array));
        }
    }
}
